package spring_boot_;

import java.lang.reflect.Method;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.diagnostics.FailureAnalyzer;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;

import spring_boot_.domain.Person;

/**
 * <a href="http://git.oschina.net/liuxia8/spring-boot-theory/wikis/spring_boot_start">spring boot启动原理解密</a>
 */
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        //loadFactoryNames();
        //stackTree();
        //stopWatch();
        loadFailureAnalyzers();
        SpringApplication.run(DemoApplication.class, args);
        //new SpringApplicationBuilder().sources(DemoApplication.class).web(false).run(args);
    }

    private static void loadFailureAnalyzers() {
        List<String> analyzerNames = SpringFactoriesLoader
                .loadFactoryNames(FailureAnalyzer.class, null);
        analyzerNames.forEach(System.out::println);
    }

    private static void stackTree() {
        StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
        for (StackTraceElement stackTraceElement : stackTrace) {
            if ("main".equals(stackTraceElement.getMethodName())) {
                try {
                    Class t = Class.forName(stackTraceElement.getClassName());
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println(stackTrace);
    }

    private static void loadFactoryNames() {
        //Method m = SpringApplication.class.getDeclaredMethod("", "");
        List<String> t = SpringFactoriesLoader.loadFactoryNames(ApplicationListener.class, Thread.currentThread().getContextClassLoader());
        t.forEach(System.out::println);
    }

    private static void stopWatch() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeSeconds());
    }

    @Bean
    public Person person() {
        return Person.builder().age(3).build();
    }

}
